#include "CM_ProcessItem.h"
#include <iostream>
using namespace sqlite3x;

CM_ProcessItem::CM_ProcessItem()
{
}

CM_ProcessItem::~CM_ProcessItem()
{
}

int
CM_ProcessItem::saveProcess(int collectionId, WMI_Result<WMI_Process>& process)
{
	try {
		vector<WMI_Process*> resulVec = process.getObjects();
		sqlite3_connection con(DEFAULT_DATABASE_NAME.c_str());
		sqlite3_transaction trans(con);
		{
			for(vector<WMI_Process*>::const_iterator it = resulVec.begin(); it != resulVec.end(); it++ )
            {
				sqlite3_command cmd(con, INSERT_PROCESS_SCRIPT);
				
				cmd.bind(1, collectionId);
			    cmd.bind(2,(*it)->getCaption());
				cmd.bind(3,(*it)->getCommandLine());
				cmd.bind(4,(*it)->getCreationClassName());
                cmd.bind(5,(*it)->getCreationDate().GetTime());
				cmd.bind(6,(*it)->getCSCreationClassName());
				cmd.bind(7,(*it)->getCSName());
				cmd.bind(8,(*it)->getDescription());
				cmd.bind(9,(*it)->getExecutablePath());
				cmd.bind(10,(*it)->getExecutionState());
				cmd.bind(11,(*it)->getHandle());
				cmd.bind(12,(*it)->getHandleCount());
				cmd.bind(13,(*it)->getInstallDate().GetTime());
				cmd.bind(14,(*it)->getKernelModeTime());
				cmd.bind(15,(*it)->getMaximumWorkingSetSize());
				cmd.bind(16,(*it)->getMinimumWorkingSetSize());
				cmd.bind(17,(*it)->getName());
				cmd.bind(18,(*it)->getOSCreationClassName());
				cmd.bind(19,(*it)->getOSName());
				cmd.bind(20,(*it)->getOtherOperationCount());
				cmd.bind(21,(*it)->getOtherTransferCount());
				cmd.bind(22,(*it)->getPageFaults());
				cmd.bind(23,(*it)->getPageFileUsage());
				cmd.bind(24,(*it)->getParentProcessId());
				cmd.bind(25,(*it)->getPeakPageFileUsage());
				cmd.bind(26,(*it)->getPeakVirtualSize());
				cmd.bind(27,(*it)->getPeakWorkingSetSize());
				cmd.bind(28,(*it)->getPriority());
				cmd.bind(29,(*it)->getPrivatePageCount());
				cmd.bind(30,(*it)->getProcessId());
				cmd.bind(31,(*it)->getQuotaNonPagedPoolUsage());
				cmd.bind(32,(*it)->getQuotaPagedPoolUsage());
				cmd.bind(33,(*it)->getQuotaPeakNonPagedPoolUsage());
				cmd.bind(34,(*it)->getQuotaPeakPagedPoolUsage());
				cmd.bind(35,(*it)->getReadOperationCount());
				cmd.bind(36,(*it)->getReadTransferCount());
				cmd.bind(37,(*it)->getSessionId());
				cmd.bind(38,(*it)->getStatus());
				cmd.bind(39,(*it)->getTerminationDate().GetTime());
				cmd.bind(40,(*it)->getThreadCount());
				cmd.bind(41,(*it)->getUserModeTime());
				cmd.bind(42,(*it)->getVirtualSize());
				cmd.bind(43,(*it)->getWindowsVersion());
				cmd.bind(44,(*it)->getWorkingSetSize());
				cmd.bind(45,(*it)->getWriteOperationCount());
				cmd.bind(46,(*it)->getWriteTransferCount());
			    cmd.executenonquery();
            }
			
		}
		trans.commit();
		con.close();
		
    }
    catch(exception &ex) {
        cerr << "Exception Occured: " << ex.what() << endl;
        return 0;
    }
	return 1;
}

int
CM_ProcessItem::getProcess(int collectionID,  WMI_Result<WMI_Process>& process)
{
	try {
 		sqlite3_connection con(DEFAULT_DATABASE_NAME.c_str());
		{
			sqlite3_command cmd(con, SELECT_PROCESS_SCRIPT);
			
			cmd.bind(1, collectionID);
			sqlite3_reader reader=cmd.executereader();
			WMI_Process * auxProcess;
			while(reader.read())
			{
				auxProcess = new WMI_Process;

				auxProcess->setCaption(wstring(reader.getstring16(2)));
				auxProcess->setCommandLine(reader.getstring16(3));
				auxProcess->setCreationClassName(reader.getstring16(4));
				auxProcess->setCreationDate(CTime(reader.getint64(5)));
				auxProcess->setCSCreationClassName(reader.getstring16(6));
				auxProcess->setCSName(reader.getstring16(7));
				auxProcess->setDescription(reader.getstring16(8));
				auxProcess->setExecutablePath(reader.getstring16(9));
				auxProcess->setExecutionState(reader.getint(10));
				auxProcess->setHandle(reader.getstring16(11));
				auxProcess->setHandleCount(reader.getint(12));
				auxProcess->setInstallDate(CTime(reader.getint64(13)));
				auxProcess->setKernelModeTime(reader.getint(14));
				auxProcess->setMaximumWorkingSetSize(reader.getint(15));
				auxProcess->setMinimumWorkingSetSize(reader.getint(16));
				auxProcess->setName(reader.getstring16(17));
				auxProcess->setOSCreationClassName(reader.getstring16(18));
				auxProcess->setOSName(reader.getstring16(19));
				auxProcess->setOtherOperationCount(reader.getint(20));
				auxProcess->setOtherTransferCount(reader.getint(21));
				auxProcess->setPageFaults(reader.getint(22));
				auxProcess->setPageFileUsage(reader.getint(23));
				auxProcess->setParentProcessId(reader.getint(24));
				auxProcess->setPeakPageFileUsage(reader.getint(25));
				auxProcess->setPeakVirtualSize(reader.getint(26));
				auxProcess->setPeakWorkingSetSize(reader.getint(27));
				auxProcess->setPriority(reader.getint(28));
				auxProcess->setPrivatePageCount(reader.getint(29));
				auxProcess->setProcessId(reader.getint(30));
				auxProcess->setQuotaNonPagedPoolUsage(reader.getint(31));
				auxProcess->setQuotaPagedPoolUsage(reader.getint(32));
				auxProcess->setQuotaPeakNonPagedPoolUsage(reader.getint(33));
				auxProcess->setQuotaPeakPagedPoolUsage(reader.getint(34));
				auxProcess->setReadOperationCount(reader.getint(35));
				auxProcess->setReadTransferCount(reader.getint(36));
				auxProcess->setSessionId(reader.getint(37));
				auxProcess->setStatus(reader.getstring16(38));
				auxProcess->setTerminationDate(CTime(reader.getint64(39)));
				auxProcess->setThreadCount(reader.getint(40));
				auxProcess->setUserModeTime(reader.getint(41));
				auxProcess->setVirtualSize(reader.getint(42));
				auxProcess->setWindowsVersion(reader.getstring16(43));
				auxProcess->setWorkingSetSize(reader.getint(44));
				auxProcess->setWriteOperationCount(reader.getint(45));
				auxProcess->setWriteTransferCount(reader.getint(46));

				process.getObjects().push_back(auxProcess);
			}
		}
		con.close();
	}
	catch(exception &ex) {
		cerr << "Exception Occured getProcess: " << ex.what() << endl;
		return 0;
	}

	return 1;
}
